//
//  HorizontalMenuView.m
//  水平栏
//
//  Created by xdd on 16/4/27.
//  Copyright © 2016年 xdd_xiaojuan. All rights reserved.
//

#import "HorizontalMenuView.h"

@implementation HorizontalMenuView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

/*
 为HorizontalMenuView显示控件
 1、根据_menuArray元素个数，for创建按钮；
 2、设置按钮标识tag；      
 3、设置按钮字体大小 颜色 状态；
 4、添加按钮点击响应事件；
 5、添加分割线、底部划线和当前被选中的标识下划线。
 */
- (void)setNameWithArray:(NSArray *)menuArray{

    self.menuArray = menuArray;
    //
    CGFloat SPACE = (self.frame.size.width - 5*5) / [self.menuArray count];
    
    for(int i = 0; i < [self.menuArray count]; i++){
     _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.frame = CGRectMake(SPACE * i + 5 *(i+1), 2, SPACE, self.frame.size.height/3*2);
        _btn.tag = i;
        _btn.layer.cornerRadius = 10;
        _btn.layer.masksToBounds = YES;
//        _btn.layer.borderColor = [UIColor grayColor].CGColor;
//        _btn.layer.borderWidth = 1.0;
        [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
//        if(_btn.tag == 0){
//            _btn.selected = YES;
//            [_btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
////
////            _btn.layer.borderColor = [UIColor greenColor].CGColor;
////            _btn.layer.borderWidth = 1.0;
//        }
//    //设置字体大小、颜色、状态
    //
        [_btn setTitle:self.menuArray[i] forState:UIControlStateNormal];//下面的替代
//        [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        [_btn setTitle:self.menuArray[i] forState:UIControlStateDisabled];
////         [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//        btn.titleLabel.font = [UIFont systemFontOfSize:15];
//        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[menuArray objectAtIndex:i]];
//        [str addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15], NSForegroundColorAttributeName:[UIColor grayColor]} range:NSMakeRange(0, [str length])];
//        [_btn setAttributedTitle:str forState:UIControlStateNormal];
//        
//        NSMutableAttributedString *selStr = [[NSMutableAttributedString alloc] initWithString:[menuArray objectAtIndex:i]];
//        [selStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15], NSForegroundColorAttributeName:[UIColor greenColor]} range:NSMakeRange(0, [str length])];
//        [_btn setAttributedTitle:selStr forState:UIControlStateDisabled];
        
        [_btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btn];
        
        //分割线
//        if (i > 0 && self.frame.size.height > 16) {
//            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(SPACE * i, 8, 1, self.frame.size.height - 16)];
//            line.backgroundColor = [UIColor whiteColor];
//            [self addSubview:line];
//        }
    }
//    \
    //底部划线
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 2.5, self.frame.size.width, 2)];
//    line.backgroundColor = [UIColor grayColor];
//    [self addSubview:line];
    
    //标注当前被选中
//    UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 1, SPACE + 1, 3)];
//    markView.tag = 999;
//    markView.backgroundColor = [UIColor orangeColor];
//    [self addSubview:markView];
    
}

/*
 
 实现按钮点击响应事件
 1、遍历获取self所有子类；
 2、判断是否是按钮类[UIButton class]；
 3、如果是UIButton则根据被点击的sender.tag 设置各个按钮的使能；
 4、根据被点击的sender.tag动画移动标识下划线。
 */
- (void)btnclick:(UIButton *)sender{
//    [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *subBtn = (UIButton *)subView;
            if (subBtn.tag == sender.tag) {
//                NSLog(@"*****%@",  subBtn.titleLabel.text);
//                subBtn.titleLabel.textColor = [UIColor redColor];
                [subBtn setTitleColor:[UIColor greenColor] forState:Normal];
            }else{
            [subBtn setTitleColor:[UIColor grayColor] forState:Normal];
//                NSLog(@"//////%@",  subBtn.titleLabel.text);
            }
        }
    }
    [self setNeedsLayout];
    if([self.delegate respondsToSelector:@selector(getTag:) ]){
        [self.delegate getTag:sender.tag];
    }
}

@end
